#include "ParseAVI.h"


int parse_avi(char* filePath, struct Meta* ret_m)
{
	FILE *fp;

	unsigned char RIFF[4];
	unsigned char FILE_SIZE[4];
	unsigned char RIFF_TYPE[4];

	unsigned char ChunkID[4];
	unsigned char ChunkSize[4];

	unsigned char HeaderType[4]={0};

	unsigned char TmpBuffer[4];

	unsigned char CodecCode[2];
	int TimePerFrame=0;
	int TotalFrame=0;

	int W = 0;
	int H = 0;
	int Duration = 0;


	int FulFllCount=0;

	char Header_Type[10]={0};

	char v_codec_buff[10];
	char a_codec_buff[10];


	char* vid_codec;
	char* aud_codec;

	int cnt = 0;
	int size= 0;

	int codec_code = 0;

	int Offset = 0;


	fp = fopen(filePath,"rb");

	if(fp == NULL){

		printf("FAILED TO OPEN FILE");
		return 0;
	}

	ret_m->fileSize = GetFileLength(fp);

	ret_m->upnpClass=(char*)malloc(strlen(UPNP_VIDEO)+1);
	strcpy(ret_m->upnpClass,UPNP_VIDEO);

	ret_m->ProtocolInfo = (char*)malloc(strlen(PROTOCOL_AVI)+1);
	strcpy(ret_m->ProtocolInfo,PROTOCOL_AVI);



	fread(RIFF,sizeof(unsigned char),(size_t)4,fp);
	fread(FILE_SIZE,sizeof(unsigned char),(size_t)4,fp);
	fread(RIFF_TYPE,sizeof(unsigned char),(size_t)4,fp);

	Offset += 12;

	while(FulFllCount < 5)
	{

		fread(ChunkID,sizeof(unsigned char),(size_t)4,fp);
		fread(ChunkSize,sizeof(unsigned char),(size_t)4,fp);

		Offset+=8;

		if(strncmp(ChunkID,"LIST",4) == 0)
		{
			fread(HeaderType,sizeof(unsigned char),(size_t)4,fp);
			Offset+=4;
		}
		else if(strncmp(ChunkID,"avih",4) == 0)
		{
			fread(TmpBuffer,sizeof(unsigned char),(size_t)4,fp);
			TimePerFrame = getIntFrom4Byte_R(TmpBuffer);
			Offset+=16;

			
			fseek(fp,Offset,SEEK_SET);

			fread(TmpBuffer,sizeof(unsigned char),(size_t)4,fp);
			TotalFrame = getIntFrom4Byte_R(TmpBuffer);

			Offset+=16;

			fseek(fp,Offset,SEEK_SET);

			fread(TmpBuffer,sizeof(unsigned char),(size_t)4,fp);
			W = getIntFrom4Byte_R(TmpBuffer);

			fread(TmpBuffer,sizeof(unsigned char),(size_t)4,fp);
			H = getIntFrom4Byte_R(TmpBuffer);

			Offset+=24;

			fseek(fp,Offset,SEEK_SET);

			Duration = TimePerFrame * TotalFrame / 1000000;

			ret_m->Duration = Duration;
			ret_m->ResolutionX = W;
			ret_m->ResolutionY = H;

			FulFllCount += 3;

		}
		else if(strncmp(ChunkID,"strh",4) == 0)
		{
			fread(TmpBuffer,sizeof(unsigned char),(size_t)4,fp);
			getStringFromBuffer(TmpBuffer,Header_Type,4);
	
			if(strncmp(Header_Type,"vids",4) == 0)
			{
	
				fread(TmpBuffer,sizeof(unsigned char),(size_t)4,fp);

				getStringFromBuffer(TmpBuffer,v_codec_buff,4);

				ret_m->VID_CODEC = (char*)malloc(strlen(v_codec_buff)+1);
				strcpy(ret_m->VID_CODEC,v_codec_buff);
				FulFllCount++;


				fseek(fp,getIntFrom4Byte_R(ChunkSize)-8,SEEK_CUR);


			}
			else
			{
				fseek(fp,getIntFrom4Byte_R(ChunkSize)-4,SEEK_CUR);

			}

		}
		else if(strncmp(ChunkID,"strf",4) == 0)
		{

			if(strncmp(Header_Type,"auds",4) == 0)
			{


				fread(CodecCode,sizeof(unsigned char),(size_t)2,fp);


				codec_code = getIntFromByte_R(CodecCode,2);
				getAudCodecFromCode(codec_code,a_codec_buff);


				ret_m->AUD_CODEC = (char*)malloc(strlen(a_codec_buff)+1);
				strcpy(ret_m->AUD_CODEC,a_codec_buff);
				
				FulFllCount++;



				fseek(fp,getIntFrom4Byte_R(ChunkSize)-2,SEEK_CUR);

			}else
				fseek(fp,getIntFrom4Byte_R(ChunkSize),SEEK_CUR);

			
		}
		else
		{
			fseek(fp,getIntFrom4Byte_R(ChunkSize),SEEK_CUR);
		}

	}

/*
	printf("\n-AVI Parsing Results.------");
	printf("File Size : %d\n",ret_m->fileSize);
	printf("Duration  : %d\n",ret_m->Duration);
	printf("Resolution: %dx%d\n",ret_m->ResolutionX,ret_m->ResolutionY);
	printf("Protocol  : %s\n",ret_m->ProtocolInfo);
	printf("UpnpClass : %s\n",ret_m->upnpClass);
	printf("V-codec   : %s\n",ret_m->VID_CODEC);
	printf("A-codec	  : %s\n",ret_m->AUD_CODEC);
	printf("-------------------------");
*/
	fclose(fp);

	return FILE_MOVIE;

}
